<script setup>
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'
import '@vueup/vue-quill/dist/vue-quill.snow.css';

import { ref } from 'vue'

//文章分类数据模型
const places = ref()

//用户搜索时选中的状态
const poiName=ref('')
const poiType=ref('')

//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(5)//每页条数

//添加表单数据模型
const placeModel = ref({
  poiId: '', //地点id
  districtName: '', //区县名称
  poiName: '', //地点名称
  distanceStr: '',  //距离
  tagList1:'',  //标签
  tagList2:'', //标签
  price:'', //价格
  latitude:'', //维度
  longitude:'', //经度
  commentScore:'', //评分
  commentCount:'', //评论数
  coverImageUrl:'', //图片
  introduction:'', //简介
  poiType: '', //地点类型
})

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  placeList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  placeList()
}

//获取文章列表
import {placeListService} from '@/api/place.js'
const placeList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    poiName: poiName.value ? poiName.value : null,
    poiType: poiType.value ? poiType.value : null
  }
  let result = await placeListService(params);

  //渲染视图
  total.value = result.data.total;
  places.value = result.data.items;
}

//添加地点
import {placeAddService} from '@/api/place.js'
import {ElMessage} from 'element-plus'
const addPlace = async ()=>{
  //调用接口
  let result = await placeAddService(placeModel.value);
  ElMessage.success(result.msg? result.msg:'添加成功');
  //让抽屉消失
  visibleDrawer.value = false;
  //刷新当前列表
  await placeList()
}

//更改地点信息
import {placeUpdateService} from "@/api/place.js";
const updatePlace = async ()=>{
  let result = await placeUpdateService(placeModel.value);
  ElMessage.success(result.msg? result.msg:'修改成功');
  //让抽屉消失
  visibleDrawer.value = false;
  //刷新当前列表
  await placeList()
}

//删除文章
import {ElMessageBox} from 'element-plus'
import {placeDeleteService} from "@/api/place.js";
const deletePlace = async (row) => {
  //提示用户 确认框
  ElMessageBox.confirm(
      '你确认要删除该地点吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //调用接口
        let result = await placeDeleteService(row.poiId);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        await placeList();
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
}
//调用函数
placeList() //获取地点列表

import {Plus} from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref()
//定义抽屉标题显示
const title = ref('')

//展示抽屉
const showDialog = (row) => {
  visibleDrawer.value = true;
  title.value = '编辑地点';
  placeModel.value.poiId = row.poiId;
  placeModel.value.districtName = row.districtName;
  placeModel.value.poiName = row.poiName;
  placeModel.value.distanceStr = row.distanceStr;
  placeModel.value.tagList1 = row.tagList1;
  placeModel.value.tagList2 = row.tagList2;
  placeModel.value.price = row.price;
  placeModel.value.latitude = row.latitude;
  placeModel.value.longitude = row.longitude;
  placeModel.value.commentScore = row.commentScore;
  placeModel.value.commentCount = row.commentCount;
  placeModel.value.coverImageUrl = row.coverImageUrl;
  placeModel.value.introduction = row.introduction;
  placeModel.value.poiType = row.poiType;
}

//导入token
import {useTokenStore} from "@/stores/token.js";

const tokenStore = useTokenStore()

const clearData = () => {
  placeModel.value.poiId = '';
  placeModel.value.districtName = '';
  placeModel.value.poiName = '';
  placeModel.value.distanceStr = '';
  placeModel.value.tagList1 = '';
  placeModel.value.tagList2 = '';
  placeModel.value.price = '';
  placeModel.value.latitude = '';
  placeModel.value.longitude = '';
  placeModel.value.commentScore = '';
  placeModel.value.commentCount = '';
  placeModel.value.coverImageUrl = '';
  placeModel.value.introduction = '';
  placeModel.value.poiType = '';
};

</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>地点管理</span>
        <div class="extra">
          <el-button type="primary" @click="visibleDrawer=true;title='添加地点';clearData()">添加地点</el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="搜索地点：">
        <el-input placeholder="请输入" v-model="poiName" style="width: 200px">
        </el-input>
      </el-form-item>
      <el-form-item label="类别：">
        <el-select placeholder="选择" v-model="poiType" style="width: 200px">
          <el-option label="全部" value=""></el-option>
          <el-option label="景点" value="sight"></el-option>
          <el-option label="酒店" value="hotels"></el-option>
          <el-option label="美食" value="food"></el-option>
          <el-option label="购物" value="shop"></el-option>
          <el-option label="娱乐" value="entertainment"></el-option>
          <el-option label="车站" value="station"></el-option>
          <el-option label="停车场" value="parking"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="placeList()">搜索</el-button>
        <el-button @click="placeList">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="places" style="width: 100%">
      <el-table-column label="id" width="100" prop="poiId"></el-table-column>
      <el-table-column label="城市" width="100" prop="districtName"></el-table-column>
      <el-table-column label="地点" prop="poiName"></el-table-column>
      <el-table-column label="评分" prop="commentScore"></el-table-column>
      <el-table-column label="评论数" prop="commentCount"> </el-table-column>
      <el-table-column label="类型" prop="poiType"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deletePlace(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
    <!-- 抽屉 -->
    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
      <!-- 添加文章表单 -->
      <el-form :model="placeModel" label-width="100px" >
        <el-form-item label="城市" >
          <el-input v-model="placeModel.districtName" placeholder="城市"></el-input>
        </el-form-item>
        <el-form-item label="地点">
            <el-input v-model="placeModel.poiName" placeholder="地点"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="placeModel.distanceStr" placeholder="地址"></el-input>
        </el-form-item>
        <el-form-item label="评分">
          <el-input v-model="placeModel.commentScore" placeholder="评分"></el-input>
        </el-form-item>
        <el-form-item label="评论数">
          <el-input v-model="placeModel.commentCount" placeholder="评论数"></el-input>
        </el-form-item>
        <el-form-item label="标签1">
          <el-input v-model="placeModel.tagList1" placeholder="标签1"></el-input>
        </el-form-item>
        <el-form-item label="标签2">
          <el-input v-model="placeModel.tagList2" placeholder="标签2"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="placeModel.price" placeholder="价格"></el-input>
        </el-form-item>
        <el-form-item label="类别">
          <el-input v-model="placeModel.poiType" placeholder="类别"></el-input>
        </el-form-item>
        <el-form-item label="经度">
          <el-input v-model="placeModel.longitude" placeholder="经度"></el-input>
        </el-form-item>
        <el-form-item label="纬度">
          <el-input v-model="placeModel.latitude" placeholder="纬度"></el-input>
        </el-form-item>
        <el-form-item label="文章封面">
          <!--
              auto-upload:设置是否自动上传
              action:设置服务器接口路径
              name:设置上传的文件字段名
              headers:设置上传的请求头
              on-success:设置上传成功的回调函数
           -->
          <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false" action="/api/image/upload" name="file" :headers="{'Authorization': tokenStore.token}" :on-success="uploadSuccess">
            <img v-if="placeModel.coverImageUrl" :src="placeModel.coverImageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon">
              <Plus />
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="地点简介">
          <div class="editor">
            <el-input
                type="textarea"
                :rows="10"
                v-model="placeModel.introduction"
                placeholder="请输入内容">
            </el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="title==='添加地点' ? addPlace(): updatePlace()">确定</el-button>
          <el-button type="info" @click="visibleDrawer=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

/* 抽屉样式 */
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>